﻿//语法甘露：
var object = //定义小写的object 基本类，用于实现最基础的方法等
{
    isA: function(aType) //一个判断类与类之间以及对象与类之间关系的基础方法
    {
        var self = this;
        while (self) {
            if (self === aType)
                return true;
            self = self.Type;
        };
        return false;
    }
};

function Class(aBaseClass, aClassDefine) //创建类的函数，用于声明类及继承关系
{
    function Classtmp() //创建类的临时函数壳
    {
        this.Type = aBaseClass; //我们给每一个类约定一个Type 属性，引用其继承的类
        for (var member in aClassDefine)
            if (aClassDefine.hasOwnProperty(member))
                this[member] = aClassDefine[member]; //复制类的全部定义到当前创建的类
    };

    Classtmp.prototype = aBaseClass;
    return new Classtmp();
};

function New(aClass, aParams) //创建对象的函数，用于任意类的对象创建
{
    function NewTmp() //创建对象的临时函数壳
    {
        this.Type = aClass; //我们也给每一个对象约定一个Type 属性，据此可以访问到对象所属的类
        if (aClass.Create)
            aClass.Create.apply(this, aParams); //我们约定所有类的构造函数都叫Create，这和DELPHI 比较相似;
    };

    NewTmp.prototype = aClass;
    return new NewTmp();
};

var colcount = 15;//列值
var roomClass = Class(object,
{
    Create: function(data, type, onclick) {
        this.data = data; // 房间数据
        this.type = type; //div颜色分组标识
        this.onclick = onclick;//关联单击事件
    },
    showBans: function () {
        var data = this.data;
        var bandiv = $("<div id='bandiv'></div>");
        bandiv.css({ display: "inline-block", "margin": "0 0 10px 0" });
        bandiv.addClass("clearfix");

        bandiv = this.appendUnitToBan(bandiv, data);
        return bandiv;
    },
    appendUnitToBan: function (ban, data) {
        //显示单元房间//显示楼盘下的单元
        var unitObj = [];
        var j;
        for (j = 0; j < data.length; j++) {
            if (unitObj[data[j].D_Unit] == undefined || unitObj[data[j].D_Unit].exist !== true) {
                //单元不同时新建div,且属当前一楼栋
                var unitDiv = $("<div id='unit" + data[j].D_Unit + "' class='unit'></div>");
                unitDiv.append("<div class='roomUnitDiv'>" + data[j].D_Unit + "</div>");
                ban.append(unitDiv);
                unitObj[data[j].D_Unit] = { exist: true, obj: unitDiv }
            }
        }
        this.appendColToUnit(unitObj, data);
        return ban;
    },
    appendColToUnit: function (unit, data) {
        //显示单元下的列
        var colObj = [];
        for (var j = 0; j < data.length; j++) {
            if (colObj[data[j].D_Unit + data[j].D_column] == undefined || colObj[data[j].D_Unit + data[j].D_column].exist !== true) {
                var colDiv = $("<div class='col' id='col" + data[j].D_Unit + data[j].D_column + "'></div>");
                var roomDivobj = $("<div class='roomDivUnit clearfix' style='line-height:30px;'>" + data[j].d_position + "</div>");
                var col1 = $("<div class='col1'></div>");//第一列
                var col2 = $("<div class='col2'></div>");//第二列
                colDiv.append(roomDivobj);
                colDiv.append(col1);
                colDiv.append(col2);
                unit[data[j].D_Unit].obj.append(colDiv);
                colObj[data[j].D_Unit + data[j].D_column] = { exist: true, obj: colDiv }
            }
        }
        this.appendRoomToCol(colObj, data);
    },
    appendRoomToCol: function (col, data) {
        var g = this;
        //显示房间
        var type = g.type;
        for (var j = 0; j < data.length; j++) {
            var cssclass = "";
            var operation = "";
            var state = parseInt(data[j].D_state);
            var monitor = data[j].D_Monitor === undefined ? 0 : parseInt(data[j].D_Monitor);
            var collection = parseInt(data[j].D_CollectionCount) + parseInt(data[j].E_CollectionCount);
            var orderstate = parseInt(data[j].D_OrderState);//协议状态
            if (type) {
                if (type === "snapped") {
                    //房间抢购
                    operation = data[j].D_ShoppingOper;
                    cssclass = window.highlightOrderRoom(data[j].D_ID, operation, state, monitor);
                } else if (type === "collection") {
                    cssclass = window.highlightAsCollection(collection);
                } else if (type === "monitor") {
                    operation = data[j].D_Operation;
                    cssclass = window.highlightOrderRoomMon(data[j].D_ID, operation, state, monitor);
                } else if (type === "guide") {
                    operation = data[j].D_Operation;
                    cssclass = window.highlightOrderRoomGui(data[j].D_ID, operation, state, monitor);
                } else if (type === "guidenew") {
                    operation = data[j].D_Operation;
                    cssclass = window.highlightOrderRoomGuiNew(data[j].D_ID, operation, state, monitor);
                } else if (type === "order") {
                    cssclass = window.highlightPactRoom(orderstate, state);
                }
            } else {
                cssclass = g.getBGColorByState(parseInt(data[j].D_state));
            }
            var id = data[j].D_ID;

            var roomdiv = $("<div style='letter-spacing: 0px;'></div>");
            roomdiv.attr("id", id);
            roomdiv.attr("class", "roomDiv " + cssclass);
            var str = data[j].D_RoomNo + "<div class='floor' onmouseover='event.cancelBubble = true'><div  class='floor_text' onmouseover='event.cancelBubble = true'>面积</div><div class='floor_value' onmouseover='event.cancelBubble = true'>" + data[j].D_FloorArea + "</div>" + "</div>";
            if (type) {
                if (type === "collection") {
                    str = data[j].D_RoomNo + "<div class='floor' onmouseover='event.cancelBubble = true'><div  class='count_text' onmouseover='event.cancelBubble = true'>人数</div><div class='count_value' onmouseover='event.cancelBubble = true'>" + collection + "</div>" + "</div>";
                }
            }
            roomdiv.html(str);
            roomdiv.append("<div rid='" + id + "' class='cover'></div>");
            if (g.onclick === true) {
            }
//            data[j].D_Price = ReplacePriceToThousand(data[j].D_Price);
//            data[j].D_Total = ReplacePriceToThousand(data[j].D_Total);
//            data[j].D_discountPrice = ReplacePriceToThousand(data[j].D_discountPrice);
//            data[j].D_discountTotal = ReplacePriceToThousand(data[j].D_discountTotal);
            window.roomData[id] = data[j];
            col[data[j].D_Unit + data[j].D_column].obj.append(roomdiv);
            if (col[data[j].D_Unit + data[j].D_column].obj.find(".col1").find(".roomDiv").length < colcount) {
                col[data[j].D_Unit + data[j].D_column].obj.find(".col1").append(roomdiv);
            } else {
                col[data[j].D_Unit + data[j].D_column].obj.find(".col2").append(roomdiv);
                col[data[j].D_Unit + data[j].D_column].obj.find(".col2").css("display", "block");
            }
        }
    },
    RoomSetClass: function(roomid) {
        var cssclass = "";
        if (window.first != null && window.first.toString() === roomid.toString()) {
            cssclass = "button-caution";
        }
        if (window.second != null && window.second.toString() === roomid.toString()) {
            cssclass = "button-primary";
        }
        if (window.three != null && window.three.toString() === roomid.toString()) {
            cssclass = "button-highlight";
        }
        if (window.four != null && window.four.toString() === roomid.toString()) {
            cssclass = "button-action";
        }
        return cssclass;
    }
    });

var roomloadend = false;
//获取所有房间，显示
function ShowRoomBox(resultdata, state, type) {
    var rooms = [];
    window.roomData = [];
    $("#allroomdiv div").remove();
    //房间数据缓存
    for (var i in resultdata) {
        if (resultdata.hasOwnProperty(i)) {
            var data = resultdata[i];
            var banname = data.D_BanName;
            if (rooms[banname] !== undefined && rooms[banname].exist) {
                rooms[banname].data.push(data);
            } else {
                var obj = {
                    exist: true,
                    text: data.D_BanName,
                    data: []
                };
                obj.data.push(data);
                rooms[banname] = obj;
            }
        }
    }
    if (state === 1) {
        if (window.ismonitor) {
            if (window.MonitorType === 1) {
                type = "guide";
            }
            else if (window.MonitorType === 2) {
                type = "monitor";
            }
            else if (window.MonitorType === 3) {
                type = "guidenew";
            }
        }
        window.showBans(rooms, type);
        roomloadend = true;
    }
    window.closelayer();
}

//显示楼栋
function showBans(data, type) {
    var index = 0;
    $("#allroomdiv").append("<div id=\"bansdiv\"></div>");
    $(document).off("dblclick", ".roomDiv");
    //显示楼栋
    for (var i in data) {
        if (data.hasOwnProperty(i)) {
            //创建楼栋按钮
            var ban = data[i];
            var botton =
                $('<div class="bandiv button button-big button-rounded" onclick="selectBan(this)"></div>');

            botton.attr("banid", "banforroom" + index);
            botton.html(ban.text);
            $("#bansdiv").append(botton); //楼栋按钮

            //房间div
            var banforroom = $("<div id='banforroom" + index + "' class='banforroom'></div>");
            banforroom.hide();
            $("#allroomdiv").append(banforroom);

            //显示房间
            showBox(ban.data, "banforroom" + index, type);
            index++;
        }
    }
    $("#bansdiv").find("div").first().click();
    var clicktimer = 0;
    //房间单元格点击事件
    $(document)
        .on(
            "dblclick",
            ".roomDiv",
            function () {
                clearTimeout(clicktimer);
                if (typeof (window.RoomClickEvent) === "function") {
                        window.RoomClickEvent(this);
                }
            }
        );
    $(document).on("click", ".roomDiv", function (e) {
        var g = this;
        var id = $(g).attr("id");
        $("#roomid").val(id);
        $(g).addClass("roomChecked");
        if (window.ismonitor) {
            ShowMessage(id);
        }
        setTimeout(function () {
            if (typeof (window.RoomOnClick) === "function") {
                $(".roomDiv").not(g).removeClass("roomChecked");
                window.RoomOnClick(g);
            }
            ShowRoomInfo(id);
            e.stopPropagation();
        }, 400);
    });
    $("div:not([class='roomChecked']) #allroomdiv").click(function () {
        $(".roomDiv").removeClass("roomChecked");
    });

    $(document).on("mouseout",
        ".roomDiv",
        function () {
            layer.close(window.tipindex);
        });
    $(document).on("mouseover",
        ".floor",
        function (e) {
            e.stopPropagation();
        });
    $(document).on("mouseover",
        ".floor_text",
        function (e) {
            e.stopPropagation();
        });
    $(document).on("mouseover",
        ".floor_value",
        function (e) {
            e.stopPropagation();
        });
}

//显示房间图形
function showBox(data, id, type) {
    var pagetype = type;

    //显示房间图形
    var roomObj = New(roomClass, [data, pagetype, true]);
    var bandiv = roomObj.showBans();
    $("#" + id).append(bandiv);

    //样式调整
    $(".roomDiv")
        .each(function () {
            $(this).addClass("button button-small button-rounded button-flat-primary");
        });
    $(".roomUnitDiv")
        .each(function () {
            $(this).addClass("button button-rounded button-caution");
            $(this).attr("disabled", "disabled");
        });
    $(".roomDivUnit")
        .each(function () {
            $(this).addClass("button button-small button-rounded button-highlight");
            $(this).attr("disabled", "disabled");
        });
}
var banfirstload = true;
//楼栋选择事件
function selectBan(obj) {
    $(obj).addClass("roomgold").removeClass("button-flat-primary");
    $(obj).siblings().removeClass('roomgold');
    //点击显示楼栋房间事件
    $(".banforroom")
        .each(function () {
            $(this).hide();
        });
    var banid = $(obj).attr("banid");
    var banname = $(obj).html();
    $("#banid").val(banid);
    $("#" + banid).show();

    $("#baninfo").html(window.housesName + "·" + $(obj).html());

    if (!banfirstload) {
        //楼栋切换事件
       // var json = window.wsorder.banchange(window.user, banname);
       // window.websocket.send(JSON.stringify(json));
    }
    banfirstload = false;
}

function ShowMessage(id) {
    var room = roomData[id];
    $(".dbanname").html(room.D_BanName);
    $(".dunit").html(room.D_Unit);
    $(".dunitname").html(room.d_position);
    $(".droomno").html(room.D_RoomNo);
    $(".droomcard").html(room.D_RoomCard);
    $(".dfloorarea").html(room.D_FloorArea);
    $(".dinnerarea").html(room.D_InnerArea);
    $(".dassessmentarea").html(room.D_AssessmentArea);
    $(".dhousemodel").html(room.D_HouseModel);
    $(".droomtype").html(room.D_RoomType);
    $(".dprice").html(room.D_Price);
    $(".dtotle").html(room.D_Total);
    $(".ddiscountprice").html(room.D_discountPrice);
    $(".ddiscounttotle").html(room.D_discountTotal);
    $(".dname").html(room.Name);
    $(".dcardno").html(room.CardNo);
    $(".dtel").html(room.Tel);
    $(".dcollectioncount").html(room.D_CollectionCount + "|" + room.E_CollectionCount);
}

//悬浮窗内容
function ShowRoomInfo(id) {
    var room = roomData[id];
    if (window.ismonitor) {
        var str = "<span class='Show'>房间: " + room.D_BanName + " " + room.D_Unit + " " + room.D_RoomNo + "室";
        if (room.D_Monitor === 0) {
            if (room.D_state === 1) {
                room.str += " 被预定";
            } else if (room.D_state === 2) {
                str += " " + room.Name + " 已购买";
            }
        } else if (room.D_Monitor === 1 || room.D_Monitor === 4) {
            str += " 引导控";
        } else if (room.D_Monitor === 2) {
            str += " 内控";
        } else if (room.D_Monitor === 3 || room.D_Monitor === 5) {
            str += " 内控翻红";
        }
        str += "</span>";
        $("#right").html(str);
    } else {
        $(".dbanname").html(room.D_BanName);
        $(".dunit").html(room.D_Unit);
        $(".dunitname").html(room.d_position);
        $(".droomno").html(room.D_RoomNo);
        $(".droomcard").html(room.D_RoomCard);
        $(".dfloorarea").html(room.D_FloorArea);
        $(".dinnerarea").html(room.D_InnerArea);
        $(".dassessmentarea").html(room.D_AssessmentArea);
        $(".dhousemodel").html(room.D_HouseModel);
        $(".droomtype").html(room.D_RoomType);
        $(".dprice").html(room.D_Price);
        $(".dtotle").html(room.D_Total);
        $(".ddiscountprice").html(room.D_discountPrice);
        $(".ddiscounttotle").html(room.D_discountTotal);
        $(".dname").html(room.Name);
        $(".dcardno").html(room.CardNo);
        $(".dtel").html(room.Tel);
        $(".dcollectioncount").html(room.D_CollectionCount + "|" + room.E_CollectionCount);
    }
}

function yin() {
    $("#imgdiv").hide();
}

    function printajax(cid, type) {
        var url = "http://" + document.location.host;
        $.ajax({
            type: "GET",
            url: "../data/PrintConfig.ashx", /* 注意后面的名字对应CS的方法名称 */
            data: {
                Action: 'getPrintJson',
                houseid: window.houseid,
                type: type,
                stepid: window.stepid,
                clientid: id,
                url: escape(url),
                rnd: Math.random()
            }, /* 注意参数的格式和名称 */
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                if (result.result === "true") {
                    sendMessage("print", JSON.stringify(result.json));
                } else {
                    top.$.ligerDialog.warn(result.msg);
                }
            }
        });
    }

    function roomprintajax(type, cid, rid) {
        var url = "http://" + document.location.host;
        $.ajax({
            type: "GET",
            url: "../data/PrintConfig.ashx", /* 注意后面的名字对应CS的方法名称 */
            data: { Action: 'getPrintJson', houseid: houseid, type: type, clientid: cid, roomid: rid, url: escape(url), rnd: Math.random() }, /* 注意参数的格式和名称 */
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                if (result.result === "true") {
                    sendMessage("print", JSON.stringify(result.json));
                } else {
                    top.$.ligerDialog.warn(result.msg);
                }
            }
        });
    }

    function titleExist(questiondata, qid) {
        var result = false;
        if (questiondata.length > 0) {
            for (var i in questiondata) {
                if (questiondata.hasOwnProperty(i)) {
                    if (questiondata[i].qid === qid) {
                        result = questiondata[i];
                    }
                }
            }
        }
        return result;
    }

//显示客户信息
    function ShowClientTableInfo() {
        var trs = '<tr><td colspan="6" class="table_title1">客户信息</td></tr>';
        trs += "<tr>";
        trs += '<td style="width: 80px;"><div align="right">姓名：</div></td>';
        trs += '<td><span id="spanclientName" class="roomSpan"></span></td>';
        trs += '<td style="width: 110px;"><div align="right">预付定金：</div></td>';
        trs += '<td><span id="spanprepaid" class="roomSpan"></span></td>';
        trs += "</tr>";
        trs += "<tr>";
        trs += '<td><div align="right">身份证：</div></td>';
        trs +=
            '<td colspan="3"><span id="spancardNo" style="width: 100%;text-align: left;" class="roomSpan"></span></td>';
        trs += "</tr>";
        trs += "<tr>";
        trs += '<td><div align="right">手机1：</div></td>';
        trs += '<td><span id="spanD_Tel" class="roomSpan"></span></td>';
        trs += '<td style="width: 110px;"><div align="right">手机2：</div></td>';
        trs += '<td><span id="spanD_Phone" class="roomSpan"></span></td>';
        trs += "</tr>";
        trs += "<tr>";
        trs += '<td><div align="right">户籍地址：</div></td>';
        trs +=
            '<td colspan="3"><span id="spanclientadr" style="width: 100%;text-align: left;" class="roomSpan"></span></td>';
        trs += "</tr>";
        trs += "<tr>";
        trs += '<td><div align="right">置业顾问：</div></td>';
        trs += '<td><span id="spanD_Real" class="roomSpan"></span></td>';
        trs += "</tr>";
        return trs;
    }
    function trim(s) {
        return s.replace(/(^\s*)|(\s*$)/g, "");
    }